const app = getApp()
const constant = require("../../../../../constant.js")
const storage = require("../../../../../utils/storage.js")

Page({

  /**
   * 页面的初始数据
   */
  data: {
    breakfastIndex: 0,
    breakfastSnackIndex: 0,
    lunchIndex: 0,
    lunchSnackIndex: 0,
    dinnerIndex: 0,
    dinnerSnackIndex: 0,
    waterIndex: 0,
    waters: ['2L','2.5L','3L','3.5L','4L','4.5L','5L','5L以上'],
    disabled: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      host: constant.api(app.globalData.pord).url,
      mobile: wx.getStorageSync(storage.keys.mobile),
      courseRoundId: options.courseRoundId,
      weeklyPlanId: options.weeklyPlanId
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    wx.showLoading({
      title: "正在加载中..."
    })

    var that = this;

    wx.request({
      url: that.data.host + '/coach/course-rounds/' + that.data.courseRoundId + '/weekly-plans/' + that.data.weeklyPlanId + '/dietary?mobile=' + that.data.mobile,
      method: 'GET',
      dataType: 'json',
      responseType: 'text',
      success: function (res) {
        if (res.data.code == "0") {
          wx.setNavigationBarTitle({
            title: res.data.data.name + '饮食'
          });

          that.setData({
            dietary: res.data.data.dietary ? res.data.data.dietary : null,
            breakfasts: res.data.data.breakfasts,
            lunchs: res.data.data.lunchs,
            dinners: res.data.data.dinners,
            snacks: res.data.data.snacks
          });

          that.initPicker();

          if(that.data.dietary) {
            if (that.data.dietary.water_intake) {
              that.setData({
                waterIntake: that.data.dietary.water_intake
              })
            }
            if (that.data.dietary.remark) {
              that.setData({
                remark: that.data.dietary.remark
              })
            }
          }

          wx.hideLoading();
        } else {
          wx.hideLoading();

          wx.showToast({
            title: res.data.message,
            icon: 'none',
            duration: 1000
          })
        }
      },
      fail: function (res) {
        wx.hideLoading();

        wx.showToast({
          title: '请求数据失败',
          icon: 'none',
          duration: 1000
        });
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  initPicker: function () {
    var that = this;
    
    var breakfastNames = [];
    for (var i = 0; i < that.data.breakfasts.length; i++) {
      breakfastNames.push(that.data.breakfasts[i].name);
      if (that.data.dietary && that.data.dietary.breakfast_template && that.data.breakfasts[i].id == that.data.dietary.breakfast_template) {
        that.setData({
          breakfastId: that.data.dietary.breakfast_template,
          breakfastDescription: that.data.dietary.breakfast_description,
          breakfastIndex: i
        })
      }
    }

    var lunchNames = [];
    for(var i = 0; i<that.data.lunchs.length; i++) {
      lunchNames.push(that.data.lunchs[i].name);
      if (that.data.dietary && that.data.dietary.lunch_template && that.data.lunchs[i].id == that.data.dietary.lunch_template) {
        that.setData({
          lunchId: that.data.dietary.lunch_template,
          lunchDescription: that.data.dietary.lunch_description,
          lunchIndex: i
        })
      }
    }

    var dinnerNames = [];
    for(var i=0; i<that.data.dinners.length; i++) {
      dinnerNames.push(that.data.dinners[i].name);
      if(that.data.dietary && that.data.dietary.dinner_template && that.data.dinners[i].id == that.data.dietary.dinner_template) {
        that.setData({
          dinnerId: that.data.dietary.dinner_template,
          dinnerDescription: that.data.dietary.dinner_description,
          dinnerIndex: i
        })
      }
    }

    var snackNames = [];
    for(var i=0; i<that.data.snacks.length; i++) {
      snackNames.push(that.data.snacks[i].name);
      if (that.data.dietary) {
        if (that.data.dietary.breakfast_snack_template && that.data.snacks[i].id == that.data.dietary.breakfast_snack_template) {
          that.setData({
            breakfastSnackId: that.data.dietary.breakfast_snack_template,
            breakfastSnackDescription: that.data.dietary.breakfast_snack_description,
            breakfastSnackIndex: i
          })
        }
        if (that.data.dietary.lunch_snack_template && that.data.snacks[i].id == that.data.dietary.lunch_snack_template) {
          that.setData({
            lunchSnackId: that.data.dietary.lunch_snack_template,
            lunchSnackDescription: that.data.dietary.lunch_snack_description,
            lunchSnackIndex: i
          })
        }
        if (that.data.dietary.dinner_snack_template && that.data.snacks[i].id == that.data.dietary.dinner_snack_template) {
          that.setData({
            dinnerSnackId: that.data.dietary.dinner_snack_template,
            dinnerSnackDescription: that.data.dietary.dinner_snack_description,
            dinnerSnackIndex: i
          })
        }
      }
    }

    for (var i = 0; i < that.data.waters.length; i++) {
      if (that.data.dietary && that.data.dietary.water_intake && that.data.waters[i] == that.data.dietary.water_intake) {
        that.setData({
          waterIndex: i
        })
      }
    }

    that.setData({
      breakfastNames: breakfastNames,
      lunchNames: lunchNames,
      dinnerNames: lunchNames,
      snackNames: snackNames
    })
  },

  bindPickerChange: function (e) {
    var that = this;

    var name = e.currentTarget.dataset.name;
    var index = e.detail.value;

    if(name == 'breakfast') {
      that.setData({
        breakfastIndex: index
      });
      if (!that.data.breakfastId || that.data.breakfastId != that.data.breakfasts[index].id) {
        that.setData({
          breakfastId: that.data.breakfasts[index].id,
          breakfastDescription: that.data.breakfasts[index].description
        });
      }
    } else if (name == 'breakfastSnack') {
      that.setData({
        breakfastSnackIndex: index
      });
      if (!that.data.breakfastSnackId || that.data.breakfastSnackId != that.data.snacks[index].id) {
        that.setData({
          breakfastSnackId: that.data.snacks[index].id,
          breakfastSnackDescription: that.data.snacks[index].description
        });
      }
    } else if (name == 'lunch') {
      that.setData({
        lunchIndex: index
      });
      if (!that.data.lunchId || that.data.lunchId != that.data.lunchs[index].id) {
        that.setData({
          lunchId: that.data.lunchs[index].id,
          lunchDescription: that.data.lunchs[index].description
        });
      }
    } else if (name == 'lunchSnack') {
      that.setData({
        lunchSnackIndex: index
      });
      if (!that.data.lunchSnackId || that.data.lunchSnackId != that.data.snacks[index].id) {
        that.setData({
          lunchSnackId: that.data.snacks[index].id,
          lunchSnackDescription: that.data.snacks[index].description
        });
      }
    } else if (name == 'dinner') {
      that.setData({
        dinnerIndex: index
      });
      if (!that.data.dinnerId || that.data.dinnerId != that.data.dinners[index].id) {
        that.setData({
          dinnerId: that.data.dinners[index].id,
          dinnerDescription: that.data.dinners[index].description
        });
      }
    } else if (name == 'dinnerSnack') {
      that.setData({
        dinnerSnackIndex: index
      });
      if (!that.data.dinnerSnackId || that.data.dinnerSnackId != that.data.snacks[index].id) {
        that.setData({
          dinnerSnackId: that.data.snacks[index].id,
          dinnerSnackDescription: that.data.snacks[index].description
        });
      }
    } else if (name == 'water') {
      that.setData({
        waterIndex: index,
        waterIntake: that.data.waters[index]
      })
    }
  },

  formSubmit: function (e) {
    var that = this;
    var data = e.detail.value;

    if (!data.breakfast_id || !data.breakfast_description) {
      wx.showToast({
        title: '早餐不能为空',
        icon: 'none',
        duration: 1000
      });
      return;
    } else if (!data.lunch_id || !data.lunch_description) {
      wx.showToast({
        title: '午餐不能为空',
        icon: 'none',
        duration: 1000
      });
      return;
    } else if (!data.dinner_id || !data.dinner_description) {
      wx.showToast({
        title: '晚餐不能为空',
        icon: 'none',
        duration: 1000
      });
      return;
    } else {
      that.setData({
        disabled: true
      });
      wx.request({
        url: that.data.host + '/coach/course-rounds/' + that.data.courseRoundId + '/weekly-plans/' + that.data.weeklyPlanId + '/edit-dietary?mobile=' + that.data.mobile,
        method: 'POST',
        data: data,
        header: {
          'content-type': 'application/json'
        },
        success: function (res) {
          if (res.data.code == "0") {
            wx.showToast({
              title: res.data.message,
              icon: 'success',
              duration: 1000,
              mask: true,
              success: function () {
                wx.navigateBack({
                  delta: 1
                });
              }
            });
          } else {
            wx.showToast({
              title: res.data.message,
              icon: 'none',
              duration: 3000,
              mask: true
            })
            that.setData({
              disabled: false
            })
          }
        },
        fail: function (res) {
          wx.showToast({
            title: '保存失败',
            icon: 'none',
            duration: 3000,
            mask: true
          })
          that.setData({
            disabled: false
          })
        }
      });
    }
  }
})